%  This file is part of Erlywog.

%  Erlywog is free software: you can redistribute it and/or modify
%  it under the terms of the GNU General Public License as published by
%  the Free Software Foundation, either version 3 of the License, or
%  (at your option) any later version.

%  Erlywog is distributed in the hope that it will be useful,
%  but WITHOUT ANY WARRANTY; without even the implied warranty of
%  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%  GNU General Public License for more details.

%  You should have received a copy of the GNU General Public License
%  along with Erlywog.  If not, see <http://www.gnu.org/licenses/>.

%  Copyright Jasm Sison 2009

-module(commentizer_controller).
-export([
	private/0
	%% other stuff
	, no_cookie_comment_form/2
	, cookie_comment_form/2
	, reply_to/2
	, select_option/4
	, show_comments/2
	, show_comment/3
	%% authentication code
 	, header/1
 	, ajax_request_json_key/1
 	, sortable_list/1
 	, javascript_deps/1
  	, image/2
%        , image_test_/0
	]).
	
-define(obfuscated_image_location_prefix, <<"/commentizer/image/">>).
	
private() -> false.

%% functional components
reply_to(A, Comments) ->
  EwcFunZip = fun(Comment_, Nr_) ->
    Title_ = comment:title(Comment_)
    , Commenter_ = comment:commenter_name(Comment_)
    , {ewc, commentizer, select_option, [A, integer_to_list(Nr_), Title_, Commenter_]}
    end
  , lists:zipwith(EwcFunZip, Comments, lists:seq(1,length(Comments))).
%   , [{ewc, commentizer, select_option, [A, integer_to_list(Number), Title, Commenter]} || Number <- lists:seq(0,30) ].
  
select_option(A, Number, Title, Commenter) ->
  {data, {Number, Title, Commenter}}.
  
show_comments(A, EntryNr) ->
  Comments = comment:find({entry_id,'=',EntryNr})
  , EwcFunZip = fun(Comment_,Nr_) -> {ewc, commentizer, show_comment, [A,Comment_, Nr_]} end
  , [ lists:zipwith(EwcFunZip, Comments, lists:seq(1,length(Comments)))
    , {ewc, commentizer, no_cookie_comment_form, [A, Comments]}
  ].

%% TODO - show if censored that it is censored
show_comment(A, Comment, CommentNr) ->
  Get = fun(Fun) -> comment:Fun(Comment) end,
  Get_ = fun (Fun) ->
    erlydb_base:field_to_iolist(comment:Fun(Comment))
  end,
  { data
    , {
      integer_to_list(CommentNr)
    , _CommentId = Get_(id)
    , _Title = Get(title)
    , _Webpage = Get(webpage)
    , _CommenterName = Get(commenter_name)
    , _Comment = Get(comment)
    , _ReplyTo = Get_(replyto_comment_id)
    , _Published = Get_(published)
  }}.

%% front-end markup
cookie_comment_form (A, Comments) ->
  CommenterName = <<"Anonymous user">>,  %% TODO - get name from cookies
  [ {ewc, commentizer, reply_to, [A, Comments] }, {data, CommenterName} ].
  
no_cookie_comment_form (A, Comments) ->
  [
    {ewc, commentizer, reply_to, [A, Comments] }
   , {ewc, commentizer, sortable_list, [A]}
  ].
  
%% authentication code
%% front-end
javascript_deps(A) ->
  H = html_blog_header_view,
  {data, [H:md5([]), H:jQuery([]), H:jQueryJSON(), H:jQueryUI()]}.

sortable_list(A) ->
  %%ImageLocations = ["ilabcd","ilefgh","ilijkl","ilmnop"], % TODO + get hashed image names set
  %%PartialKeys = ["pkabcd","pkefgh","pkijkl","pkmnop"],    % TODO - get keys from key_manager, and partitition into equal nr of images
  ImageLocations = [ [?obfuscated_image_location_prefix, Image] || Image <- image_manager:get_random_image_set()]
  , PartialKeys = ["a","b","c","d"]
  , {data, {ImageLocations, PartialKeys}}.

header(A) ->
  {data, []}.
  
image(A, Md5String) ->
  ImageLocation = image_manager:get_image_location(list_to_binary(Md5String)),
  {response, {page, ImageLocation}}.

%% back-end
ajax_request_json_key(A) ->
  case yaws_arg:method(A) of
   'POST' ->
      Output = {data, <<"Wahey!">>}
      %% TODO - get name of commenter from post args
      , blog_util:ajax_escape(Output)
   ;_ ->
     {ewr, blog}
  end.

%% <<Size:8, String:Size/binary-unit:8>> = <<5, "hello">>. 
%% website: http://pupeno.com/blog/erlang-the-language-for-network-programming-issue-1
